﻿@page "/plugin"
@inherits ListComponent<CmPlugin>

<PageTitle>插件市场 - @AppConfig.AppTitle</PageTitle>

<div class="container cl-container">
    <AntForm Id="query">
        <CmsCard>
            <div class="cl-query-item">
                <label>分类：</label>
                <CmsCheckList Id="@nameof(CmPlugin.Category)" Value="@category" Items="CodeTable.PluginCategories" OnClick="OnCategoryClick" />
            </div>
            <div class="cl-query-item">
                <label>类型：</label>
                <CmsCheckList Id="@nameof(CmPlugin.Type)" Value="@type" Items="CodeTable.PluginTypes" OnClick="OnTypeClick" />
            </div>
        </CmsCard>
        <div style="height:20px;"></div>
        <CmsCard>
            <HeadTemplate>
                <CmsTitle Icon="far fa-list" Title="插件列表" />
                <div style="width:200px;">
                    <AntSearch Id="Key" Placeholder="请输入关键字搜索" Value="@key" />
                </div>
            </HeadTemplate>
            <ChildContent>
                @if (Result.TotalCount == 0)
                {
                    <AntEmpty />
                }
                else
                {
                    <div class="cl-plugins">
                        @foreach (var item in Result.PageData)
                        {
                            <UPluginItem Item="item" />
                        }
                    </div>
                    <div style="padding:15px 0;text-align:center;">
                        <AntPagination TotalCount="Result.TotalCount" PageSize="Criteria.PageSize" PageIndex="Criteria.PageIndex" />
                    </div>
                }
            </ChildContent>
        </CmsCard>
    </AntForm>
</div>

<script>
    function cl_query_plugin(id, value) {
        document.getElementById(id).value = value || '';
        return document.getElementById('query').submit();
    }
</script>

@code {
    private IPluginService Service;
    private string category;
    private string type;
    private string key;

    protected override async Task OnInitAsync()
    {
        await base.OnInitAsync();
        Service = await CreateServiceAsync<IPluginService>();
        category = Context.Request.Get<string>(nameof(CmPlugin.Category));
        type = Context.Request.Get<string>(nameof(CmPlugin.Type));
        key = Context.Request.Get<string>("Key");
        Criteria.SetQuery(nameof(CmPlugin.Category), QueryType.Batch, category);
        Criteria.SetQuery(nameof(CmPlugin.Type), QueryType.Equal, type);
        Criteria.Parameters["Key"] = key;
        Result = await Service.QueryPluginsAsync(Criteria);
    }

    private string OnCategoryClick(string value = null)
    {
        value = value ?? "";
        return $"cl_query_plugin('{nameof(CmPlugin.Category)}','{value}')";
    }

    private string OnTypeClick(string value = null)
    {
        value = value ?? "";
        return $"cl_query_plugin('{nameof(CmPlugin.Type)}','{value}')";
    }
}